// pages/guest/guest.js
const { PageBottomPadding } = require('../../utils/index.js');

Page({
  data: {
    pageBottomStyle: '',
    allRooms: [],
    pinnedRooms: [],
    recentRooms: [],
    totalRooms: 0,
    activeRooms: 0,
    refreshing: false,
    showRoomActionSheet: false,
    currentRoom: null,
    hasLoaded: false,
    roomActions: [
      { name: '进入房间', value: 'enter' },
      { name: '分享房间', value: 'share' },
      { name: '置顶/取消置顶', value: 'pin' },
      { name: '删除房间', value: 'delete' }
    ],
    // 主要功能（房间相关）
    mainFeatures: [
      {
        key: 'quick-order',
        title: '快速点单',
        subtitle: '智能进入',
        image: '/images/order.png',
        highlight: true
      },
      {
        key: 'create-room',
        title: '创建房间',
        subtitle: '开始点餐',
        image: '/images/order.png',
        highlight: false
      },
      {
        key: 'join-room',
        title: '加入房间',
        subtitle: '输入邀请码',
        image: '/images/orders.png',
        highlight: false
      }
    ],
    
    // 次要功能
    secondaryFeatures: [
      {
        key: 'room-history',
        title: '房间历史',
        desc: '查看历史房间',
        image: '/images/profile.png'
      }
    ],
    
    // 蒙层状态
    showOverlay: false,
    overlayMessage: ''
  },

  onLoad() {
    // 设置游客模式
    this.setGuestMode();
    // 加载模拟数据
    this.loadMockData();
    this.setPageBottomPadding();
  },

  onShow() {
    // 每次显示页面时刷新数据
    this.setPageBottomPadding();
  },

  // 设置游客模式
  setGuestMode() {
    const app = getApp();
    app.globalData.isGuestMode = true;
    app.globalData.isLoggedIn = false;
    
    // 设置游客用户信息
    app.globalData.userInfo = {
      id: 'guest',
      nickname: '游客用户',
      avatar: 'https://img.yzcdn.cn/vant/cat.jpeg',
      phone: null
    };
  },

  // 加载模拟数据
  loadMockData() {
    // 模拟房间数据
    const mockRooms = [
      {
        room_id: 'mock_1',
        roomId: 'mock_1',
        title: '周末聚餐',
        type: 'permanent',
        status: 'active',
        members_count: 8,
        current_round: 1,
        total_orders: 15,
        is_pinned: true,
        last_active: Date.now() - 1000 * 60 * 30, // 30分钟前
        description: '周末和朋友们的聚餐'
      },
      {
        room_id: 'mock_2',
        roomId: 'mock_2',
        title: '公司团建',
        type: 'temporary',
        status: 'active',
        members_count: 12,
        current_round: 2,
        total_orders: 28,
        is_pinned: false,
        last_active: Date.now() - 1000 * 60 * 60, // 1小时前
        description: '公司年度团建活动'
      },
      {
        room_id: 'mock_3',
        roomId: 'mock_3',
        title: '朋友聚会',
        type: 'temporary',
        status: 'active',
        members_count: 6,
        current_round: 1,
        total_orders: 9,
        is_pinned: true,
        last_active: Date.now() - 1000 * 60 * 15, // 15分钟前
        description: '老朋友聚会'
      },
      {
        room_id: 'mock_4',
        roomId: 'mock_4',
        title: '家庭聚餐',
        type: 'permanent',
        status: 'closed',
        members_count: 4,
        current_round: 1,
        total_orders: 7,
        is_pinned: false,
        last_active: Date.now() - 1000 * 60 * 60 * 2, // 2小时前
        description: '家庭聚餐'
      },
      {
        room_id: 'mock_5',
        roomId: 'mock_5',
        title: '生日派对',
        type: 'temporary',
        status: 'active',
        members_count: 15,
        current_round: 1,
        total_orders: 22,
        is_pinned: false,
        last_active: Date.now() - 1000 * 60 * 5, // 5分钟前
        description: '小明的生日派对'
      },
      {
        room_id: 'mock_6',
        roomId: 'mock_6',
        title: '部门聚餐',
        type: 'permanent',
        status: 'expired',
        members_count: 10,
        current_round: 3,
        total_orders: 18,
        is_pinned: false,
        last_active: Date.now() - 1000 * 60 * 60 * 24, // 1天前
        description: '技术部门聚餐'
      }
    ];

    // 分离置顶和普通房间
    const pinnedRooms = mockRooms.filter(room => room.is_pinned);
    const recentRooms = mockRooms.filter(room => !room.is_pinned);

    this.setData({
      allRooms: mockRooms,
      pinnedRooms: pinnedRooms,
      recentRooms: recentRooms,
      totalRooms: mockRooms.length,
      activeRooms: mockRooms.filter(room => room.status === 'active').length,
      hasLoaded: true
    });
  },

  // 设置页面底部间距
  setPageBottomPadding() {
    const style = PageBottomPadding.getStyle();
    this.setData({
      pageBottomStyle: style
    });
  },

  // 下拉刷新
  onRefresh() {
    this.setData({ refreshing: true });
    // 模拟刷新
    setTimeout(() => {
      this.loadMockData();
      this.setData({ refreshing: false });
    }, 1000);
  },

  // 功能点击处理
  onFeatureClick(e) {
    const { feature } = e.currentTarget.dataset;
    this.showGuestOverlay('登录后可使用此功能');
  },

  // 房间点击（游客模式）
  onRoomClick(e) {
    this.showGuestOverlay('登录后可进入房间');
  },

  // 游客操作（所有操作都显示蒙层）
  onGuestAction(e) {
    this.showGuestOverlay('登录后可使用此功能');
  },

  // 显示游客模式蒙层
  showGuestOverlay(message) {
    this.setData({
      showOverlay: true,
      overlayMessage: message
    });
  },

  // 隐藏蒙层
  hideOverlay() {
    this.setData({
      showOverlay: false,
      overlayMessage: ''
    });
  },

  // 去登录
  goToLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    });
  },

  // 房间操作菜单选择
  onRoomActionSelect(e) {
    this.showGuestOverlay('登录后可管理房间');
  },

  // 关闭房间操作菜单
  onCloseRoomActionSheet() {
    this.setData({ showRoomActionSheet: false });
  },

  // 蒙层点击处理
  onOverlayClick() {
    this.hideOverlay();
  },

  // 蒙层内容点击（阻止冒泡）
  onOverlayContentClick(e) {
    // 阻止事件冒泡
    if (e && e.stopPropagation) {
      e.stopPropagation();
    }
  }
});
